package com.token.iot.bratterycharging.gateway.sender;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.outstanding.framework.core.Logger;
import com.outstanding.framework.core.PendingException;
import com.outstanding.framework.log.LoggerFactory;
import com.token.iot.bratterycharging.gateway.AbstractSender;
import com.token.iot.bratterycharging.gateway.ChannelHandlerContextManager;
import com.token.iot.bratterycharging.gateway.IDGeneration;
import com.token.iot.bratterycharging.gateway.ResCode;
import com.token.iot.bratterycharging.gateway.Semaphore;
import com.token.iot.bratterycharging.gateway.SemaphoreManager;
import com.token.iot.bratterycharging.gateway.Sender;
import com.token.iot.bratterycharging.gateway.utils.ByteUtil;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.Attribute;

/**
 * 获取连接地址
 * 
 * @author chenzhixuan
 *
 */
@Component("AABSender")
public class AABSender extends AbstractSender {

	private static ChannelHandlerContextManager manager = new ChannelHandlerContextManager();

	private static Logger loger = LoggerFactory.getLogger(AABSender.class);

	@Override
	public Object send(String imei, Map<String, String> params) throws PendingException {
		// 查找对应的HandlerContext
		ChannelHandlerContext context = manager.getChannelHandlerContextByImei(imei);

		// 锁住context
		synchronized (context) {
			Attribute<Long> attr = context.channel().attr(attributeTIME);
			Long time = null;
			time = attr.get();
			if (time != null) {
				if (System.currentTimeMillis() < time + 2 * 1000) {
					try {
						Thread.sleep(1500);
					} catch (InterruptedException e) {
						loger.error(e);
					}
				}
			}

			attr.set(System.currentTimeMillis());
		}
		String sessionid = IDGeneration.createSeesionID();

		String command = code + sessionid + "/";
		byte[] c = ByteUtil.addFlag(command.getBytes());
		context.writeAndFlush(c);
		String key = imei + "@" + sessionid;
		// 信号量
		Semaphore semaphore = SemaphoreManager.registerCommand(key, this);
		try {
			semaphore.await();
		} catch (InterruptedException e) {
			loger.error(e);
			throw new PendingException(ResCode.semaphoreTimeOut);
		}
		// 得到回复
		String response = semaphore.getResponse();
		loger.info("The address Mes =" + response);
		int post = response.indexOf("#/#");
		int lastpost = response.indexOf("#/#");
		int portpost = response.indexOf("$$$");
		String fixedAddress = response.substring(13, post);

		String fixedport = response.substring(post + 3, portpost);
		String configAddress = null;
		String configPort = null;
		if (portpost == -1) {
			configAddress = response.substring(portpost + 3);
		} else {
			configAddress = response.substring(portpost + 3, lastpost);
			configPort = response.substring(lastpost + 3);

		}
		loger.info("The fixedAddress  =" + fixedAddress);
		loger.info("The fixedport  =" + fixedport);
		loger.info("The configAddress  =" + configAddress);
		loger.info("The configPort  =" + configPort);

		return fixedAddress;

	}

}
